<template>
  <div class="page-container">
    <el-card class="main-card">
      <div slot="header" class="card-header">
        <span>入库管理</span>
        <div class="header-operations">
          <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增入库单</el-button>
        </div>
      </div>

      <!-- 搜索栏 -->
      <div class="search-section">
        <el-form :inline="true" :model="searchForm" class="search-form">
          <el-form-item label="入库单号">
            <el-input v-model="searchForm.inboundNo" placeholder="请输入入库单号" clearable></el-input>
          </el-form-item>
          <el-form-item label="入库类型">
            <el-select v-model="searchForm.type" placeholder="请选择入库类型" clearable>
              <el-option label="采购入库" value="purchase"></el-option>
              <el-option label="退货入库" value="return"></el-option>
              <el-option label="调拨入库" value="transfer"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="入库时间">
            <el-date-picker
              v-model="searchForm.dateRange"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">查询</el-button>
            <el-button @click="resetSearch">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 入库单列表 -->
      <div class="table-section">
        <el-table
          :data="inboundList"
          style="width: 100%"
          v-loading="loading"
        >
          <el-table-column prop="inboundNo" label="入库单号" width="180"></el-table-column>
          <el-table-column prop="type" label="入库类型" width="120">
            <template slot-scope="scope">
              <el-tag>{{ getTypeText(scope.row.type) }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="productCount" label="商品数量" width="100"></el-table-column>
          <el-table-column prop="totalAmount" label="总金额" width="120"></el-table-column>
          <el-table-column prop="warehouseName" label="入库仓库" width="150"></el-table-column>
          <el-table-column prop="operator" label="操作人" width="120"></el-table-column>
          <el-table-column prop="status" label="状态" width="100">
            <template slot-scope="scope">
              <el-tag :type="getStatusType(scope.row.status)">
                {{ getStatusText(scope.row.status) }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>
          <el-table-column label="操作" width="200" fixed="right">
            <template slot-scope="scope">
              <el-button 
                type="text" 
                size="small" 
                @click="handleView(scope.row)"
                :disabled="scope.row.status === 'draft'"
              >查看</el-button>
              <el-button 
                type="text" 
                size="small" 
                @click="handleEdit(scope.row)"
                :disabled="scope.row.status !== 'draft'"
              >编辑</el-button>
              <el-button 
                type="text" 
                size="small" 
                @click="handleDelete(scope.row)"
                :disabled="scope.row.status !== 'draft'"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="pagination-container">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'InboundManagement',
  data() {
    return {
      searchForm: {
        inboundNo: '',
        type: '',
        dateRange: []
      },
      loading: false,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      inboundList: [
        {
          inboundNo: 'IN202403150001',
          type: 'purchase',
          productCount: 100,
          totalAmount: '¥89,990',
          warehouseName: '主仓库',
          operator: '张三',
          status: 'completed',
          createTime: '2024-03-15 10:30:00'
        },
        {
          inboundNo: 'IN202403150002',
          type: 'return',
          productCount: 5,
          totalAmount: '¥4,999',
          warehouseName: '主仓库',
          operator: '李四',
          status: 'draft',
          createTime: '2024-03-15 14:20:00'
        }
      ]
    }
  },
  methods: {
    handleAdd() {
      console.log('新增入库单')
    },
    handleSearch() {
      this.loading = true
      this.fetchData()
    },
    resetSearch() {
      this.searchForm = {
        inboundNo: '',
        type: '',
        dateRange: []
      }
      this.handleSearch()
    },
    handleView(row) {
      console.log('查看', row)
    },
    handleEdit(row) {
      console.log('编辑', row)
    },
    handleDelete(row) {
      console.log('删除', row)
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    fetchData() {
      setTimeout(() => {
        this.loading = false
        this.total = 100
      }, 500)
    },
    getTypeText(type) {
      const typeMap = {
        purchase: '采购入库',
        return: '退货入库',
        transfer: '调拨入库'
      }
      return typeMap[type] || type
    },
    getStatusType(status) {
      const statusMap = {
        draft: 'info',
        processing: 'warning',
        completed: 'success',
        cancelled: 'danger'
      }
      return statusMap[status] || 'info'
    },
    getStatusText(status) {
      const statusMap = {
        draft: '草稿',
        processing: '处理中',
        completed: '已完成',
        cancelled: '已取消'
      }
      return statusMap[status] || status
    }
  },
  mounted() {
    this.fetchData()
  }
}
</script>

<style scoped>
.page-container {
  padding: 20px;
  background: #f0f2f5;
  min-height: calc(100vh - 120px);
}

.main-card {
  border-radius: 8px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #304156;
}

.search-section {
  margin-bottom: 20px;
}

.search-form {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
}

.table-section {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

/* Element UI 主题覆盖 */
:deep(.el-button--primary) {
  background-color: #ff6b00;
  border-color: #ff6b00;
}

:deep(.el-button--primary:hover),
:deep(.el-button--primary:focus) {
  background-color: #ff8533;
  border-color: #ff8533;
}

:deep(.el-button--text) {
  color: #ff6b00;
}

:deep(.el-button--text:hover),
:deep(.el-button--text:focus) {
  color: #ff8533;
}

.el-card {
  border-radius: 8px;
}

.el-card__header {
  padding: 15px 20px;
  border-bottom: 1px solid #e6e6e6;
}

.el-table {
  color: #304156;
}

.el-table th {
  background-color: #f5f7fa;
  color: #304156;
}

:deep(.el-tag) {
  border-radius: 4px;
}

:deep(.el-form-item__label) {
  color: #304156;
}

:deep(.el-input__inner) {
  border-radius: 4px;
}

:deep(.el-input__inner:focus) {
  border-color: #ff6b00;
}

:deep(.el-select .el-input__inner:focus) {
  border-color: #ff6b00;
}

:deep(.el-tag--success) {
  background-color: #f0f9eb;
  border-color: #e1f3d8;
  color: #67c23a;
}

:deep(.el-tag--warning) {
  background-color: #fdf6ec;
  border-color: #faecd8;
  color: #e6a23c;
}

:deep(.el-tag--danger) {
  background-color: #fef0f0;
  border-color: #fde2e2;
  color: #f56c6c;
}

:deep(.el-tag--info) {
  background-color: #f4f4f5;
  border-color: #e9e9eb;
  color: #909399;
}
</style> 